package sse.ustc.cc.custom;

import sse.ustc.cc.activity.R;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;
import android.widget.TextView;
/**
 * 结束选择dialog
 * @author xuhao
 *
 */
@SuppressLint("NewApi") 
public class SectionChooseDialog extends Dialog{
	
	private TextView weekplay = null;//课程在周几上课
	private TextView jieshustartplay = null;//课程开始节数
	private TextView jieshuendplay = null;//课程结束节数
	
	private NumberPicker week = null;//星期几选择
	private NumberPicker start_jieshu = null;//开始节数选择
	private NumberPicker end_jieshu = null;//结束节数选择
	
	/** 设置默认节数*/
	private String weekChoosed = "周一";
	private int jieshuStartNum = 1;
	private int jieshuEndNum = 1;
	
	Context context;
	public SectionChooseDialog(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public SectionChooseDialog(Context context, int theme){
        super(context, theme);
        this.context = context;
    }
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(false);//点击对话框外禁止取消窗口
        setContentView(R.layout.jieshuchoosed);
        
        weekplay = (TextView)findViewById(R.id.weekplay);
        jieshustartplay = (TextView)findViewById(R.id.jieshustartplay);
        jieshuendplay = (TextView)findViewById(R.id.jieshuendplay);
        //初始化
        weekplay.setText(weekChoosed);
        jieshustartplay.setText(jieshuStartNum+"");
        jieshuendplay.setText(jieshuEndNum+"");
        
        week = (NumberPicker)findViewById(R.id.week);
        start_jieshu = (NumberPicker)findViewById(R.id.start_jieshu);
        end_jieshu = (NumberPicker)findViewById(R.id.end_jieshu);
        //初始化weekplay数据
        final String[] weekdata = {"周一","周二","周三","周四","周五","周六","周日"};
        week.setDisplayedValues(weekdata);
        week.setMinValue(0);  
        week.setMaxValue(weekdata.length - 1);
        //设置start_jieshu的最小值和最大值
        start_jieshu.setMinValue(1);
        start_jieshu.setMaxValue(15);
        //设置end_jieshu的最小值和最大值
        end_jieshu.setMinValue(1);
        end_jieshu.setMaxValue(15);
        //设置start_jieshu的当前值
        start_jieshu.setValue(1);
        end_jieshu.setValue(1);
        //设置周的响应事件
        week.setOnValueChangedListener(new OnValueChangeListener(){

            @Override
            public void onValueChange(NumberPicker picker, int oldVal,
                    int newVal) {
                // TODO Auto-generated method stub
            	weekplay.setText(weekdata[newVal]);
            	setWeekChoosed(weekdata[newVal]);
            }
            
        });
        //设置开始节数的响应事件
        start_jieshu.setOnValueChangedListener(new OnValueChangeListener(){

            @Override
            public void onValueChange(NumberPicker picker, int oldVal,
                    int newVal) {
                // TODO Auto-generated method stub
            	jieshustartplay.setText(newVal+"");
            	setJieshuStartNum(newVal);
            }
            
        });
        //设置结束节数的响应事件
        end_jieshu.setOnValueChangedListener(new OnValueChangeListener(){

            @Override
            public void onValueChange(NumberPicker picker, int oldVal,
                    int newVal) {
                // TODO Auto-generated method stub
            	if(newVal<jieshuStartNum){
            		jieshuendplay.setError("结束节数要大于开始节数。");
            		jieshuendplay.setText(jieshuStartNum+"");
            		setJieshuEndNum(jieshuStartNum);
            		return;
            	}
            	jieshuendplay.setText(newVal+"");
            	setJieshuEndNum(newVal);
            }
            
        });
        
        
    }

	public String getWeekChoosed() {
		return weekChoosed;
	}

	public void setWeekChoosed(String weekChoosed) {
		this.weekChoosed = weekChoosed;
	}

	public int getJieshuStartNum() {
		return jieshuStartNum;
	}

	public void setJieshuStartNum(int jieshuStartNum) {
		this.jieshuStartNum = jieshuStartNum;
	}

	public int getJieshuEndNum() {
		return jieshuEndNum;
	}

	public void setJieshuEndNum(int jieshuEndNum) {
		this.jieshuEndNum = jieshuEndNum;
	}
	
}
